export class AuthStorage {
  static KEY = "__AUTH_TOKEN__";
  static LOGIN_INFO_KEY = "__LOGIN_INFO__";
  static CURRENT_BRANCH_KEY = "__CURRENT_BRANCH__";
  static OWN_BRANCHES_KEY = "__OWN_BRANCHES__";

  static setAuthToken(value: string) {
    localStorage.setItem(this.KEY, value);
  }

  static getAuthToken() {
    const value = localStorage.getItem(this.KEY);
    return value || "";
  }

  static setLoginInfo(loginInfo: any) {
    window.localStorage.setItem(this.LOGIN_INFO_KEY, JSON.stringify(loginInfo));
  }

  static getLoginInfo() {
    let loginInfoStr = window.localStorage.getItem(this.LOGIN_INFO_KEY) || "";
    let loginInfo = {};
    try {
      loginInfo = JSON.parse(loginInfoStr);
    } catch (e) {
      console.error("UserInfo 缓存转换失败");
    }
    return loginInfo;
  }

  static setCurrentBranch(branchInfo: any) {
    window.localStorage.setItem(this.CURRENT_BRANCH_KEY, JSON.stringify(branchInfo));
  }

  static getCurrentBranch() {
    let branchInfoStr = window.localStorage.getItem(this.CURRENT_BRANCH_KEY) || "";
    let branchInfo = {};
    try {
      branchInfo = JSON.parse(branchInfoStr);
    } catch (e) {
      console.error("BranchInfo 缓存转换失败");
    }
    return branchInfo;
  }

  static setOwnBranches(branchList: any) {
    window.localStorage.setItem(this.OWN_BRANCHES_KEY, JSON.stringify(branchList));
  }

  static getOwnBranches() {
    let branchListStr = window.localStorage.getItem(this.OWN_BRANCHES_KEY) || "";
    let branchList = [];
    try {
      branchList = JSON.parse(branchListStr);
    } catch (e) {
      console.error("BranchList 缓存转换失败");
    }
    return branchList;
  }
}
